#include "tedeng.h"
#include "ui_tedeng.h"
#include <QTimer>
#include <QDebug>
#include<QPushButton>
#include<QMovie>
#include<QPropertyAnimation>
#include <QSoundEffect>

tedeng::tedeng(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::tedeng)
    , scene(new QGraphicsScene(this))
    , teBackground(nullptr)

{
    ui->setupUi(this);

    setuptdBk();
}

tedeng::~tedeng()
{
    delete scene;
    delete ui;
    qDeleteAll(soundEffects);
    soundEffects.clear();
}
void tedeng::setuptdBk()
{
    // 初始化图形视图
    ui->graphicsView->setScene(scene);
    setWindowTitle("艾露猫：老大给你弹竖琴听喵");
    // 窗口大小（物理像素）
    this->setFixedSize(1200, 760); //1800/1.5, 1140/1.5 和缩放抵消
    // graphicsView 大小（逻辑像素，与窗口一致）
    ui->graphicsView->setFixedSize(1200, 760);
    // 设置场景大小（逻辑像素，与背景图一致）
    scene->setSceneRect(0, 0, 1800, 1140);
    // 调整 graphicsView 的变换矩阵，使 1800x1140 的场景适配 1200x760 的视图
    ui->graphicsView->setTransform(QTransform::fromScale(1200.0/1800.0, 760.0/1140.0));

    teBackground = new QGraphicsPixmapItem(QPixmap(":/image/C:/Users/lenovo/Pictures/tedeng.jpg"));//背景
    teBackground->setPos(0, 0);  // 明确设置位置
    teBackground->setZValue(-10);
    scene->addItem(teBackground);


    QMovie *movie = new QMovie(":/image/C:/Users/lenovo/Pictures/1_IMG_5776.GIF");
    movie->start();

    ui->label->setMovie(movie);

    playBtn = new QPushButton("开始演奏", this);
    playBtn ->setGeometry(750, 400, 100, 60);
    playBtn ->setStyleSheet("font-size: 20px;");
    connect(playBtn, &QPushButton::clicked, this, &tedeng::play);

    // 音乐文件路径
    soundPaths = {
        ":/music/C:/Users/lenovo/Music/1.wav",
        ":/music/C:/Users/lenovo/Music/2.wav",
        ":/music/C:/Users/lenovo/Music/3.wav",
        ":/music/C:/Users/lenovo/Music/4.wav",
        ":/music/C:/Users/lenovo/Music/5.wav",
        ":/music/C:/Users/lenovo/Music/6.wav",
        ":/music/C:/Users/lenovo/Music/7.wav"
    };

    setupMusicBtns();
}
void tedeng::setupMusicBtns()
{
    // 7个音乐按钮
    QStringList buttonLabels = {"Do", "Re", "Mi", "Fa", "Sol", "La", "Si"};
    for (int i = 0; i < 7; i++) {
        QPushButton *btn = new QPushButton(buttonLabels[i], this);
        btn->setGeometry(300 + i * 80, 400, 70, 70);
        btn->setStyleSheet("font-size: 16px;");
        btn->hide();
        connect(btn, &QPushButton::clicked, this, &tedeng::MusicClick);
        musicBtns.append(btn);

        QSoundEffect *effect = new QSoundEffect(this);
        effect->setSource(QUrl::fromLocalFile(soundPaths[i]));
        effect->setVolume(0.8f);
        soundEffects.append(effect);
    }
}

//槽
void tedeng::play()
{
    playBtn->hide();
    for (QPushButton *btn : musicBtns) {
        btn->show(); //显示
    }
}

void tedeng::MusicClick()
{
    QPushButton *btn = qobject_cast<QPushButton*>(sender());
    int index = musicBtns.indexOf(btn);
    if (index >= 0 && index < soundPaths.size()) {
        soundEffects[index]->play();
    }
}

